﻿namespace Dissemination.MVC.Repositories
{
    using System.Linq;
    using System.Collections.Generic;
    using Dissemination.MVC.Models;

    public class ReporterRepository : BaseRepository<Reporter>
    {
        public ReporterRepository()
        {
            EntityName = "Reporters";
        }

        public override List<Reporter> List()
        {
            return dataContext.Reporters.ToList();
        }

        public List<Gender> ListGenders()
        {
            return dataContext.Genders.ToList();
        }

        public List<Reporter> ListByTargetAreaID(int targetAreaID)
        {
            var reporters = (from r in dataContext.Reporters
                             select r);
            return reporters.ToList();
        }

        // We must use these methods because we are using .NET Framework 3.5
        // Using .NET Framework 4.0 foreign keys are kept automatically

        public Gender GetGenderByID(int genderID)
        {
            return dataContext.Genders.FirstOrDefault(g => g.GenderID == genderID);
        }

        public Program GetProgramByTargetAreaID(int targetAreaID)
        {
            return dataContext.TargetAreas.Include("Program").FirstOrDefault(ta => ta.TargetAreaID == targetAreaID).Program;
        }

        public TargetArea GetTargetAreaByID(int targetAreaID)
        {
            return dataContext.TargetAreas.Include("Program").FirstOrDefault(ta => ta.TargetAreaID == targetAreaID);
        }

        // End comment

        public override Reporter Get(int id)
        {
            return dataContext.Reporters.Include("Gender").Include("TargetArea.Program").FirstOrDefault(r => r.ReporterID == id);
        }

    }
}
